package com.onlineMIS.ORM.DAO.headQ.barCodeGentor;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQInputHelp;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagGroup;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadqEntityTag;


@Repository
public class HeadQEntityTagDaoImpl extends BaseDAO<HeadqEntityTag> {

	@Autowired
	private HeadQTagDaoImpl headQTagDaoImpl;
	@Autowired
	private HeadQTagGroupDaoImpl headQTagGroupDaoImpl;
	
	/**
	 * 根據tagId 查找headqEntityTag
	 * @param i
	 * @return
	 */
	public List<HeadqEntityTag> findByTagId(int tagId){
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("tagId", tagId));
		List<HeadqEntityTag> tags = getByCritera(criteria, true);
		return tags;
	}
	
	
	
	/**
	 * 根據custId 查找headqEntityTag
	 * @param i
	 * @return
	 */
	public List<Integer> findByEntityId(int entityId){
		List<Integer> list = new ArrayList<Integer>();
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("entityId", entityId));
		List<HeadqEntityTag> tags = getByCritera(criteria, true);
		if (tags !=null) {
			for (HeadqEntityTag headqEntityTag : tags) {
				list.add(headqEntityTag.getTagId());
			}
		}
		return list;
	}
	
	
   /**
	* 根據custId 查找headqEntityTag
	* @param i
    * @return
	*/
	public List<HeadqEntityTag> selectByEntityId(int entityId){
			DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
			criteria.add(Restrictions.eq("entityId", entityId));
			List<HeadqEntityTag> tags = getByCritera(criteria, true);
			return tags;
	}
	
	
	/**
	 * 根据客户id查找客户标签集合
	 * @param i
	 * @return
	 */
	public List<Integer> selectTagsByEntityId(int entityId){
		List<Integer> list = new ArrayList<Integer>();
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("entityId", entityId));
		List<HeadqEntityTag> tags = getByCritera(criteria, true);
		if (tags != null) {
			for (HeadqEntityTag headqEntityTag : tags) {
				list.add(headqEntityTag.getTagId());
			}
		}
		// 供应商标签集合
		List<Integer> supplierTags = headQTagDaoImpl.selectSupplierTags();
		List<Integer> comm = new ArrayList<Integer>(list);
		//剔除供应商标签
        comm.retainAll(supplierTags);
        list.removeAll(supplierTags);
		return list;
	}
	
	
	
	/**
	 * 根据供应商id查找供应商标签集合
	 * @param i
	 * @return
	 */
	public List<Integer> selectTagsBySupplierId(int entityId){
		List<Integer> list = new ArrayList<Integer>();
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("entityId", entityId));
		List<HeadqEntityTag> tags = getByCritera(criteria, true);
		if (tags != null) {
			for (HeadqEntityTag headqEntityTag : tags) {
				list.add(headqEntityTag.getTagId());
			}
		}
		// 客户标签集合
		List<Integer> custTags = headQTagDaoImpl.selectCustTags();
		List<Integer> comm = new ArrayList<Integer>(list);
		//剔除客户标签
        comm.retainAll(custTags);
        list.removeAll(custTags);
		return list;
	}
	

	
	
	/**
	 * 根據custId 查找headqEntityTag
	 * @param i
	 * @return
	 */
	public HeadqEntityTag selectEntityTag(int entityId,int tagId){
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("entityId",entityId));
		criteria.add(Restrictions.eq("tagId",tagId));
		List<HeadqEntityTag> tags = getByCritera(criteria, true);
		if (tags.size() !=0) {
			return tags.get(0);
		}else {
			return null;
		}
		
	}




	/**
	 * 判断该客户是否存在该等级标签
	 * @return
	 */
	public Boolean isExistence(int entityId,int tagId) {	
		
		//根据标签id获取分组id
		HeadQTag headQTag = headQTagDaoImpl.get(tagId, true);
		// 根据分组id 获取分组信息
		HeadQTagGroup hGroup = headQTagGroupDaoImpl.get(headQTag.getGroupId().getId(), true);
			
		List<Integer> list = new ArrayList<Integer>();
		// 获取客户标签集合
		List<Integer> custIds = findByEntityId(entityId);
		// 拿到该分组的标签集合
		List<Integer> headQTags = headQTagDaoImpl.selectTagsByGroupId(hGroup.getId());
		
		list.addAll(custIds);
		list.retainAll(headQTags);
		if (list.size() > 0) {
			return true;
		}else {
			return false;
		}
		
	}
	
	
	/**
	 * 根据客户id,获取客户标签集合
	 * @param custId
	 * @return
	 */
	public List<Integer> selectTagsByCustId(int custId) {	
		List<Integer> list = new ArrayList<Integer>();
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("entityId",custId));
		List<HeadqEntityTag> tags = getByCritera(criteria, true);
		if (tags != null) {
			for (HeadqEntityTag headqEntityTag : tags) {
				list.add(headqEntityTag.getTagId());
			}			
		}
		 return list;
	}



	public List<HeadqEntityTag> getEntityTagByTagId(int tagId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqEntityTag.class);
		criteria.add(Restrictions.eq("tagId",tagId));
		return getByCritera(criteria, true);
		
	}
	
	
	
	



}
